from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools.utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts """
figType = [".png", ".eps"]
darkGreen = Color(0, 170, 0)
purple = Color(128, 0, 128)
darkGold = Color(255, 193, 37)
colors = [Color.blue, Color.red, darkGreen, Color.magenta, Color.black, purple, darkGold]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFontZoom = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names """
rangeVal = [2.5, 6]
rangeZoom = [2.95, 2.98]

evRelNoPhotFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/evRelNoPhot"
firstPhotFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/firstPhot"
indPhotFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/indPhot"
photFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/phot"
evRelFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/evRel"

outPath = "/home/marc-antoine/Documents/Dropbox/BGO"
outName = "SingleEvent300K_V2"


""" 
###############################################################
# Create the P1D
###############################################################
"""
baseline = P1D("Baseline", evRelNoPhotFile, style="p", color=colors[2], symbol=5, symbolSize=2, errAll=False)

baselineZoom = P1D("Baseline", evRelNoPhotFile, style="p", color=colors[2], symbol=5, symbolSize=6, errAll=False)

firstPhot = P1D("First Photon", firstPhotFile, style="p", color=colors[2], symbol=10, symbolSize=14, errAll=False)

indivPhot = P1D("Photons", indPhotFile, style="p", color=colors[1], symbol=6, symbolSize=8, errAll=False)

phot = P1D("phot", photFile, style="p", color=colors[4], symbol=4, symbolSize=4, errAll=False)

evRel = P1D("evRel", evRelFile, style="l", color=colors[0], penWidth=2, symbol=4, symbolSize=0, errAll=False)

evRelZoom = P1D("evRel", evRelFile, style="lp", color=colors[0], penWidth=2, symbol=4, symbolSize=4, errAll=False)


"""
###############################################################
# Plotting Part
###############################################################
"""

baselineKey = JaKey(baseline.symbol, baseline.title, 0.165, 0.675, "NDC", color=baseline.color, keyColor=baseline.color, penWidth=2, font=legendFont)

firstPhotKey = JaKey(firstPhot.symbol, firstPhot.title, 0.165, 0.725, "NDC", color=firstPhot.color, keyColor=firstPhot.color, penWidth=2, font=legendFont)

indivPhotKey = JaKey(indivPhot.symbol, indivPhot.title, 0.165, 0.775, "NDC", color=indivPhot.color, keyColor=indivPhot.color, penWidth=2, font=legendFont)



""" Create the Plotter """
mainPlotter = HPlotJa("Single Event", 600, 600, 2, 1, True)
mainPlotter.setGTitle("Scintillation Event in BGO with ^{22}Na at 300 K", titleFont)
mainPlotter.setAntiAlias(False)
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
mainPlotter.cd(1,1)
mainPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
mainPlotter.setRange(rangeVal[0], rangeVal[1], -0.85, 0.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.setTicFont(ticFont)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("Time (&micro;s)")
mainPlotter.setNameY("Amplitude (V)")

mainPlotter.cd(2,1)
mainPlotter.setPad(2, 1, 0.465, 0.455, 0.35, 0.35)
mainPlotter.setRange(rangeZoom[0], rangeZoom[1], -0.5, 0.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.setTicFont(ticFontZoom)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("")
mainPlotter.setNameY("")

""" Plots all the JHep objects """
mainPlotter.cd(1,1)
mainPlotter.draw(evRel)
#mainPlotter.draw(baseline)
#mainPlotter.draw(phot)
mainPlotter.draw(indivPhot)
mainPlotter.draw(firstPhot)
#mainPlotter.draw(baselineKey)
mainPlotter.draw(indivPhotKey)
mainPlotter.draw(firstPhotKey)

mainPlotter.cd(2,1)
mainPlotter.draw(evRelZoom)
#mainPlotter.draw(baselineZoom)
#mainPlotter.draw(phot)
mainPlotter.draw(indivPhot)
mainPlotter.draw(firstPhot)


""" Modifies the JaAxes and the axes labels """
mainPlotter.cd(1,1)
theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
theAxes_11.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_11)


mainPlotter.cd(2,1)
theAxes_21 = mainPlotter.getPad()
theAxes_21.setLabelSpace(0, 0.0)
theAxes_21.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_21)


mainPlotter.cd(1,1)
labX_11 = mainPlotter.getLabelX()[0][0]
labX_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX_11, mainPlotter, 0.035)
labY_11 = mainPlotter.getLabelY()[0][0]
labY_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY_11, mainPlotter, -0.01)

mainPlotter.update()

""" Saves or not the plot """
if writeFigures:
	for i in range(len(figType)):
		outFileName = outName + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

